<template>
  <main>
    <el-form :model="form" class="demo-form-inline">
      <el-form-item label="键">
        <el-input v-model="form.key" disabled/>
      </el-form-item>
      <el-row>
        <el-col :span="11">
          <el-form-item label="TTL">
            <el-input v-model="form.ttl"/>
          </el-form-item>
        </el-col>
        <el-col :span="2"/>
        <el-col :span="11">
          <el-form-item label="数据类型">
            <el-input v-model="form.type" disabled/>
          </el-form-item>
        </el-col>
      </el-row>
      <div v-if="form.type === 'string'">
        <el-form-item label="值">
          <el-input type="textarea" :autosize="{ minRows: 6 }" v-model="form.value" placeholder=""/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="updateKey">保存</el-button>
        </el-form-item>
      </div>
    </el-form>
  </main>
</template>

<script setup>
import {ref, watch} from 'vue'
import {ElNotification} from "element-plus";
import {GetKeyValue, UpdateKeyValue} from "../../wailsjs/go/main/App.js";

const form = ref({})
let props = defineProps(['keyDB', 'keyConnIdentity', 'keyKey'])

watch(() => props.keyKey, () => {
  getTheValue()
})

function getTheValue() {
  GetKeyValue({conn_identity: props.keyConnIdentity, db: props.keyDB, key: props.keyKey}).then(res => {
    if (res.code !== 200) {
      ElNotification({
        title: res.msg,
        type: "error",
      })
      return
    }
    form.value = res.data
    form.value.key = props.keyKey
  })
}

function updateKey() {
  UpdateKeyValue({
    conn_identity: props.keyConnIdentity,
    key: props.keyKey,
    db: props.keyDB,
    value: form.value.value,
    ttl: form.value.ttl * 1000000000
  }).then(res => {
    if (res.code !== 200) {
      ElNotification({
        title: res.msg,
        type: "error",
      })
      return
    }
    ElNotification({
      title: res.msg,
      type: "success",
    })
  })
}
</script>

<style scoped>

</style>